package com.hyj.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hyj.service.RolesService;
import com.hyj.mapper.RolesMapper;
import com.hyj.mapper.UserRolesMapper;
import com.hyj.entity.Fenye;
import com.hyj.entity.RoleModules;
import com.hyj.entity.Roles;
import com.hyj.entity.UserRoles;
import com.hyj.util.CommonUtil;

@Service
public class RolesServiceImp implements RolesService {
	@Autowired
	private RolesMapper rolesmapper;
	@Autowired
	private UserRolesMapper userRolesmapper;

	@Override
	public List<Roles> selectRoles(List<Integer> list) {
		// TODO Auto-generated method stub
		return rolesmapper.selectRoles(list);
	}

	@Override
	public Fenye<Roles> selectAllRoles(Fenye<Roles> fenye) {
		Integer total = rolesmapper.selectCountRoles(fenye);
		List<Roles> selectAllRoles = rolesmapper.selectAllRoles(fenye);
		fenye.setRows(selectAllRoles);
		fenye.setTotal(total);
		return fenye;
	}

	@Override
	public Integer addRoles(Roles roles) {
		// TODO Auto-generated method stub
		Integer addRoles=null;
		List<Roles> selectByName = rolesmapper.selectByName(roles);
		if(selectByName.size()==0) {
			addRoles = rolesmapper.addRoles(roles);
		}else {
			addRoles=-1;
		}
		 return addRoles;
	}


	@Override
	public Integer updateRoles(Roles roles) {
		// TODO Auto-generated method stub
		return rolesmapper.updateRoles(roles);
	}
	
	@Override
	public List<Roles> selectrolebyid(Integer id) {
		// TODO Auto-generated method stub
		List<UserRoles> selectUserRoles = userRolesmapper.selectUserRoles(id);
		List<Integer> urlist = new ArrayList<Integer>();
		for (int i = 0; i < selectUserRoles.size(); i++) {
			urlist.add(selectUserRoles.get(i).getRoleId());
		}
		List<Roles> selectRoles = rolesmapper.selectRoles(urlist);

		return selectRoles;
	}
	 
	@Override
	public Map<String, Object> delRoles(Integer id) {
		// TODO Auto-generated method stub
		Map<String, Object> map = CommonUtil.getResultMap();
		List<RoleModules> selectRoleModulesByRoleId = rolesmapper.selectRoleModulesByRoleId(id);
		if(selectRoleModulesByRoleId.size()!=0) {
			map.put("success", false);
			map.put("message", "该角色已被分配权限，不可删除！！！");
		}else {
			rolesmapper.delRoles(id);
			map.put("success", true);
			map.put("message", "删除成功！！！");
		}
		return map;
	}

}
